package com.yunbiao.ybbusstopplate.doublescreen;

import android.content.Context;
import android.filterfw.core.Frame;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.VideoView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.UiMessageUtils;
import com.sprylab.android.widget.TextureVideoView;
import com.yunbiao.ybbusstopplate.utils.BitmapRecycle;
import com.yunbiao.ybbusstopplate.utils.GlideLoadUtils;

public class AdvertView extends FrameLayout {
    private static final String TAG = "AdvertView";
    
    private ImageView imageView;
    private TextureVideoView videoView;
    private BitmapRecycle bitmapRecycle;
    private FrameLayout linearLayout;

    public static final int IMAGE = 1;
    public static final int VIDEO = 2;
    public static final int DESTROY = 3;

    public AdvertView(@NonNull Context context) {
        super(context);

        UiMessageUtils.getInstance().addListener(IMAGE,uiMessageCallback);
        UiMessageUtils.getInstance().addListener(VIDEO,uiMessageCallback);

        initView();
        initListener();
    }

    private void initView(){
        bitmapRecycle = new BitmapRecycle();

        linearLayout = new FrameLayout(getContext());

        imageView = new ImageView(getContext());
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        videoView = new TextureVideoView(getContext());

        LayoutParams videoLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        videoLayoutParams.gravity = Gravity.CENTER;

        LayoutParams imageLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        imageLayoutParams.gravity = Gravity.CENTER;

        linearLayout.addView(imageView,imageLayoutParams);
        linearLayout.addView(videoView,videoLayoutParams);

        addView(linearLayout,LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);

        Log.d(TAG, "initView: ");
    }

    private void initListener(){
        videoView.setOnPreparedListener(mp -> {
            Log.d(TAG, "setOnPreparedListener: ---------------");
            mp.setLooping(true);
            mp.start();
        });
        videoView.setOnErrorListener((mp, what, extra) -> {
            Log.d(TAG, "initListener: " + what + " --- " + extra);
            return true;
        });

        Log.d(TAG, "initListener: ");
    }

    private final UiMessageUtils.UiMessageCallback uiMessageCallback = new UiMessageUtils.UiMessageCallback() {
        @Override
        public void handleMessage(@NonNull UiMessageUtils.UiMessage localMessage) {
            String path = localMessage.getObject().toString();
            Log.d(TAG, "handleMessage: " + localMessage.getId() + " --- " + path);
            switch (localMessage.getId()) {
                case VIDEO:
                    playVideo(path);
                    break;
                case IMAGE:
                    playImage(path);
                    break;
                case DESTROY:
                    stop();
                    break;
            }
        }
    };

    private void playImage(String path){
        linearLayout.setBackgroundColor(Color.BLACK);

        Log.d(TAG, "playVideo: ");
        if(videoView.isPlaying()){
            videoView.stopPlayback();
        }
        if(videoView.isShown()){
            videoView.setVisibility(View.GONE);
        }

        GlideLoadUtils.getInstance().loadLocalImage(getContext(),path,imageView,linearLayout,"0",bitmapRecycle);
        imageView.setVisibility(View.VISIBLE);
    }

    private void playVideo(String path){
        linearLayout.setBackgroundColor(Color.BLACK);

        Log.d(TAG, "playImage: ");
        if(imageView.isShown()){
            imageView.setVisibility(View.GONE);
        }

        videoView.setVideoPath(path);
        videoView.setVisibility(View.VISIBLE);
        videoView.start();
    }

    private void stop(){
        if(videoView.isPlaying()){
            videoView.stopPlayback();
        }
        if(videoView.isShown()){
            videoView.setVisibility(View.GONE);
        }
        if(imageView.isShown()){
            imageView.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stop();
        UiMessageUtils.getInstance().removeListener(IMAGE,uiMessageCallback);
        UiMessageUtils.getInstance().removeListener(VIDEO,uiMessageCallback);
    }
}
